package com.wing.product.controller;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseProduct;
import com.wing.product.model.request.WarehouseProductForm;
import com.wing.product.model.response.WarehouseProductInventoryVO;
import com.wing.product.model.response.WarehouseProductVO;
import com.wing.product.service.WarehouseProductService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* (warehouse_product)表控制层
*
* @author xxxxx
*/
@Api(value = "云仓", tags = "#/warehouse/product/product")
@RestController("WarehouseProductController")
@RequestMapping("/warehouse/product")
@RequiredArgsConstructor
public class WarehouseProductController {
/**
* 服务对象
*/
    private final WarehouseProductService warehouseProductService ;


    @ApiOperation(value = "查询所有客户端", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseProductVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "99", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order
            ,@RequestParam(value = "notIds[]", required = false) List<Integer> notIds
            ,@RequestParam(value = "productIds[]", required = false) List<Integer> productIds
            ,@RequestParam(value = "productCodes[]", required = false) List<String> productCodes
            ,Long customerId
            ,String productTypeName
            ,String productProducerName
            ,String productMarkName
            ,String productVarietyName
            ,String productSpecCode
            ,String productCode
            ,String productName
            ,Long productId
            ,Long productVarietyId
            ,Long productMainUnitId
            ,Long productMaterialId
    ){
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseProduct> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("customerId", customerId);
        paramMap.put("notIds", notIds);
        paramMap.put("productIds", productIds);
        paramMap.put("productCodes", productCodes);
        paramMap.put("productTypeName", productTypeName);
        paramMap.put("productProducerName", productProducerName);
        paramMap.put("productMarkName", productMarkName);
        paramMap.put("productVarietyName", productVarietyName);
        paramMap.put("productSpecCode", productSpecCode);
        paramMap.put("productCode", productCode);
        paramMap.put("productName", productName);
        paramMap.put("productId", productId);
        paramMap.put("productVarietyId", productVarietyId);
        paramMap.put("productMainUnitId", productMainUnitId);
        paramMap.put("productMaterialId", productMaterialId);
        //搜索
        IPage<WarehouseProductVO> list = warehouseProductService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加产品", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(WarehouseProductForm.Add.class) WarehouseProductForm warehouseProductForm)  {
        try{
            Boolean checkCode=warehouseProductService.checkProductCode(warehouseProductForm.getProductCode());
            if(!checkCode){
                return JsonResult.fail("产品料号重复");
            }
            warehouseProductService.save(warehouseProductForm);
            return JsonResult.success("添加成功");
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "修改产品", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PutMapping("/update")
    public JsonResult update(@Validated(WarehouseProductForm.Update.class) WarehouseProductForm warehouseProductForm)  {
        try{
            warehouseProductService.update(warehouseProductForm);
            return JsonResult.success("添加成功");
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查询产品", notes = "查询")
    @GetMapping ("/selectProduct")
    public JsonResult<List<WarehouseProductVO>> selectProduct(@RequestParam String warehouseSignsOrderId){
        if (!StringUtils.isEmpty(warehouseSignsOrderId)){
            List<WarehouseProductVO> warehouseProductVOList = warehouseProductService.selectProduct(warehouseSignsOrderId);
            if (CollectionUtils.isNotEmpty(warehouseProductVOList)){
                return  JsonResult.success("请求成功",warehouseProductVOList);
            }else {
                return  JsonResult.success("数据为空",warehouseProductVOList);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "查询产品明细", notes = "查询")
    @GetMapping ("/selectProductDetail")
    public JsonResult<List<WarehouseProductInventoryVO>> selectProductDetail(Integer status, Long qualityTestingId){
        if (!ObjectUtils.isEmpty(qualityTestingId)){
            List<WarehouseProductInventoryVO> warehouseProductVOList = warehouseProductService.selectProductDetail(status,qualityTestingId);
            if (CollectionUtils.isNotEmpty(warehouseProductVOList)){
                return  JsonResult.success("请求成功",warehouseProductVOList);
            }else {
                return  JsonResult.success("数据为空",warehouseProductVOList);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "查询领料记录产品", notes = "查询")
    @GetMapping ("/selectRecord")
    public PageResult<WarehouseProductInventoryVO> selectRecord(@RequestParam String id,@RequestParam String warehouseId
            ,@RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "bundleCodeList[]", required = false) List<String> bundleCodeList) {
        if (!StringUtils.isEmpty(id)) {
            return warehouseProductService.selectRecord(id, warehouseId, bundleCodeList, page, limit);
        } else {
            return null;
        }
    }
}
